package com.yuan.springcloud.user.bean.open;

import com.yuan.springcloud.common.exception.ErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * @author YouYuan
 * @date 2022/11/21 2:39
 */
@Data
@ApiModel("开放平台接口返回类")
public class OpenResult<T> implements Serializable {

    /**
     * 日志跟踪标识
     */
    private static final String TRACE_ID = "TRACE_ID";

    @ApiModelProperty(value = "日志跟踪标识")
    private String traceId = MDC.get(TRACE_ID);

    @ApiModelProperty(value = "是否成功")
    private boolean success = true;
    @ApiModelProperty(value = "返回信息")
    private String message;
    @ApiModelProperty(value = "返回对象")
    private T data;
    @ApiModelProperty(value = "错误编号")
    private String errCode;
    @ApiModelProperty(value = "错误信息")
    private String errMsg;

    @ApiModelProperty(value = "签名")
    private String sign;
    @ApiModelProperty(value = "签名时间戳，毫秒")
    private Long timestamp;
    @ApiModelProperty(value = "签名随机字符串")
    private String randomStr;

    public static <T> OpenResult<T> success(T data) {
        OpenResult<T> rs = new OpenResult<>();
        rs.data = data;
        rs.success = Boolean.TRUE;
        return rs;
    }

    public static <T> OpenResult<T> success(T data, String message) {
        OpenResult<T> rs = new OpenResult<>();
        rs.data = data;
        rs.success = Boolean.TRUE;
        rs.message = message;
        return rs;
    }

    public static <T> OpenResult<T> success() {
        OpenResult<T> rs = new OpenResult<>();
        rs.success = Boolean.TRUE;
        return rs;
    }

    public static <T> OpenResult<T> error(T data, String errCode, String errMsg) {
        OpenResult<T> rs = new OpenResult<>();
        rs.data = data;
        rs.success = Boolean.FALSE;
        rs.errCode = errCode;
        rs.errMsg = errMsg;
        return rs;
    }

    public static <T> OpenResult<T> error(String errCode, String errMsg) {
        OpenResult<T> rs = new OpenResult<>();
        rs.success = Boolean.FALSE;
        rs.errCode = errCode;
        rs.errMsg = errMsg;
        return rs;
    }
    public static <T> OpenResult<T> error(String errMsg) {
        OpenResult<T> rs = new OpenResult<>();
        rs.success = Boolean.FALSE;
        rs.errCode = ErrorEnum.DEFAULT_ERR.getErrCode();
        rs.errMsg = errMsg;
        return rs;
    }

    public static <T> OpenResult<T> error(ErrorEnum errorEnum) {
        OpenResult<T> rs = new OpenResult<>();
        rs.success = Boolean.FALSE;
        if (errorEnum != null) {
            rs.errCode = errorEnum.getErrCode();
            rs.errMsg = errorEnum.getMessage();
        }
        return rs;
    }

    public static <T> OpenResult<T> error(ErrorEnum errorEnum, String errMsg) {
        OpenResult<T> rs = new OpenResult<>();
        rs.success = Boolean.FALSE;
        rs.errMsg = errMsg;
        if (errorEnum != null) {
            if (errMsg == null) {
                rs.errMsg = errorEnum.getMessage();
            }
            rs.errCode = errorEnum.getErrCode();
        }
        return rs;
    }
}
